//
//  AutoAgainLoanPopUpView.m
//  自动复借 - 弹窗
//
//  Created by wxp on 2025/5/22.
//

#import "AutoAgainLoanPopUpView.h"

@implementation AutoAgainLoanPopUpView

#pragma mark - lazy
- (UIView *)viewBack{
    if (!_viewBack) {
        _viewBack = [UIView new];
        _viewBack.backgroundColor = COLOR_WHITE;
        _viewBack.width = SCREEN_WIDTH-LEFT_MARDIN*2;
        [GlobalMethod setRoundView:_viewBack color:[UIColor clearColor] numRound:16 width:0];
        _viewBack.userInteractionEnabled = true;
    }
    return _viewBack;
}
- (UILabel *)labelTitle{
    if (!_labelTitle) {
        _labelTitle = [UILabel new];
        [GlobalMethod setLabel:_labelTitle widthLimit:0 isBold:true fontNum:16 textColor:COLOR_BLACK text:@"Préstamo automático"];
    }
    return _labelTitle;
}
- (UILabel *)labelContent{
    if (!_labelContent) {
        _labelContent = [UILabel new];
        _labelContent.font = [UIFont systemFontOfSize:14];
        _labelContent.textAlignment = NSTextAlignmentCenter;
        _labelContent.numberOfLines = 0;
        _labelContent.lineSpace = 4;
        _labelContent.textColor = [UIColor colorWithHexString:@"444444"];
    }
    return _labelContent;
}
- (UILabel *)labelMoneyTitle{
    if (!_labelMoneyTitle) {
        _labelMoneyTitle = [UILabel new];
        [GlobalMethod setLabel:_labelMoneyTitle widthLimit:0 isBold:true fontNum:14 textColor:COLOR_BLACK text:@"Préstamo automático"];
    }
    return _labelMoneyTitle;
}
- (UILabel *)labelMoney{
    if (!_labelMoney) {
        _labelMoney = [UILabel new];
        _labelMoney.font = [UIFont boldSystemFontOfSize:24];
        _labelMoney.textColor = COLOR_BLACK;
    }
    return _labelMoney;
}
- (UILabel *)labelBankTitle{
    if (!_labelBankTitle) {
        _labelBankTitle = [UILabel new];
        [GlobalMethod setLabel:_labelBankTitle widthLimit:0 isBold:true fontNum:14 textColor:COLOR_BLACK text:@"Cuenta de recepción:"];
    }
    return _labelBankTitle;
}
- (UILabel *)labelBankNameTitle{
    if (!_labelBankNameTitle) {
        _labelBankNameTitle = [UILabel new];
        [GlobalMethod setLabel:_labelBankNameTitle widthLimit:0 isBold:false fontNum:14 textColor:[UIColor colorWithHexString:@"#7E8BA5"] text:@"Nombre del banco"];
    }
    return _labelBankNameTitle;
}
- (UILabel *)labelBankName{
    if (!_labelBankName) {
        _labelBankName = [UILabel new];
        _labelBankName.font = [UIFont boldSystemFontOfSize:14];
        _labelBankName.textColor = COLOR_BLACK;
    }
    return _labelBankName;
}
- (UILabel *)labelBankCodeTitle{
    if (!_labelBankCodeTitle) {
        _labelBankCodeTitle = [UILabel new];
        _labelBankCodeTitle.numberOfLines = 0;
        _labelBankCodeTitle.lineSpace = 4;
        _labelBankCodeTitle.font = [UIFont systemFontOfSize:14];
        _labelBankCodeTitle.textColor = [UIColor colorWithHexString:@"7E8BA5"];
        [_labelBankCodeTitle fitTitle:@"Número de\ntarjeta bancaria" variable:0];
    }
    return _labelBankCodeTitle;
}
- (UILabel *)labelBankCode{
    if (!_labelBankCode) {
        _labelBankCode = [UILabel new];
        _labelBankCode.font = [UIFont boldSystemFontOfSize:14];
        _labelBankCode.textColor = COLOR_BLACK;
    }
    return _labelBankCode;
}
- (UILabel *)labelDesc{
    if (!_labelDesc) {
        _labelDesc = [UILabel new];
        _labelDesc.textColor = [UIColor colorWithHexString:@"#101010"];
        _labelDesc.font = [UIFont systemFontOfSize:12];
        _labelDesc.textAlignment = NSTextAlignmentCenter;
        _labelDesc.numberOfLines = 0;
        _labelDesc.lineSpace = 4;
    }
    return _labelDesc;
}
- (UIButton *)btnAhora{
    if (!_btnAhora) {
        _btnAhora = [UIButton new];
        _btnAhora.backgroundColor = COLOR_PRIMARY;
        _btnAhora.widthHeight = XY(self.viewBack.width-LEFT_MARDIN*2, 40);
        [GlobalMethod setRoundView:_btnAhora color:[UIColor clearColor] numRound:40/2 width:0];
        [_btnAhora setTitleColor:COLOR_BLACK forState:UIControlStateNormal];
        _btnAhora.titleLabel.font = [UIFont systemFontOfSize:14];
        [_btnAhora setTitle:@"Pagar ahora" forState:UIControlStateNormal];
        _btnAhora.tag = 1;
        [_btnAhora addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _btnAhora;
}
- (UIButton *)btnAgreement{
    if (!_btnAgreement) {
        _btnAgreement = [UIButton new];
        _btnAgreement.widthHeight = XY(32, 32);
        [_btnAgreement setImage:[UIImage imageNamed:@"icon_agreement_unselect"] forState:UIControlStateNormal];
        [_btnAgreement setImage:[UIImage imageNamed:@"icon_agreement_select"] forState:UIControlStateSelected];
        _btnAgreement.selected = true;
        _btnAgreement.tag = 2;
        [_btnAgreement addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _btnAgreement;
}
- (UILabel *)labelAgreement{
    if (!_labelAgreement) {
        _labelAgreement = [UILabel new];
        _labelAgreement.font = [UIFont systemFontOfSize:12];
        _labelAgreement.textColor = [UIColor colorWithHexString:@"#73767D"];
    }
    return _labelAgreement;
}

#pragma mark - 初始化
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.widthHeight = XY(SCREEN_WIDTH, SCREEN_HEIGHT);
        self.backgroundColor = [UIColor colorWithHexString:@"#000000" alpha:0.4];
        [self addSubView];
    }
    return self;
}

- (void)addSubView{
    [self addSubview:self.viewBack];
    [self.viewBack addSubview:self.labelTitle];
    [self.viewBack addSubview:self.labelContent];
    [self.viewBack addSubview:self.labelMoneyTitle];
    [self.viewBack addSubview:self.labelMoney];
    
    [self.viewBack addSubview:self.labelBankTitle];
    [self.viewBack addSubview:self.labelBankNameTitle];
    [self.viewBack addSubview:self.labelBankName];
    [self.viewBack addSubview:self.labelBankCodeTitle];
    [self.viewBack addSubview:self.labelBankCode];
    
    [self.viewBack addSubview:self.labelDesc];
    [self.viewBack addSubview:self.btnAhora];
    [self.viewBack addSubview:self.btnAgreement];
    [self.viewBack addSubview:self.labelAgreement];
    
    [self resetView:nil];
}

#pragma mark - 点击事件
- (void)btnClick:(UIButton *)sender{
    [NSObject clickFeedback];
    [sender addClickAnimate];
    switch (sender.tag) {
        case 1:
        {
            if(self.blockConfirmClick){
                self.blockConfirmClick();
            }
        }
            break;
        case 2:
        {
            sender.selected = !sender.selected;
            [self resetAgreement];
            if (self.blockAgreementClick) {
                self.blockAgreementClick(sender.selected);
            }
        }
            
        default:
            break;
    }
}

#pragma mark - 刷新View
- (void)resetView:(ModelOrderDetail *)model{
    self.labelTitle.centerXTop = XY(self.viewBack.width/2, 20);
    
    [self.labelContent fitTitle:@"Cuando liquide este pedido, automáticamente\nsolicitaremos el próximo préstamo de este\nproducto para usted, y podrá recibir los\nfondos rápidamente." variable:0];
    self.labelContent.centerXTop = XY(self.viewBack.width/2, self.labelTitle.bottom+12);
    
    self.labelMoneyTitle.centerXTop = XY(self.viewBack.width/2, self.labelContent.bottom+18);
    [self.labelMoney fitTitle:[NSString stringWithFormat:@"$%@",[GlobalMethod showMoney:strDot2F(model.settledAutoApplyAmount)]] variable:0];
    self.labelMoney.centerXTop = XY(self.viewBack.width/2, self.labelMoneyTitle.bottom+10);
    
    self.labelBankTitle.leftTop = XY(LEFT_MARDIN, self.labelMoney.bottom+12);
    self.labelBankNameTitle.leftTop = XY(self.labelBankTitle.left, self.labelBankTitle.bottom+12);
    
    [self.labelBankName fitTitle:readStr(self.bankName) variable:0];
    self.labelBankName.rightCenterY = XY(self.viewBack.width-self.labelBankTitle.left, self.labelBankNameTitle.centerY);
    
    self.labelBankCodeTitle.leftTop = XY(self.labelBankTitle.left, self.labelBankNameTitle.bottom+12);
    [self.labelBankCode fitTitle:readStr(self.bankCard) variable:0];
    self.labelBankCode.rightCenterY = XY(self.labelBankName.right, self.labelBankCodeTitle.centerY);
    
    
    self.labelDesc.width = self.viewBack.width-100;
    NSString * strMark = @"99%";
    NSString * str = [NSString stringWithFormat:@"La tasa de aprobación de la solicitud de préstamo automático es de hasta el %@",strMark];
    [self.labelDesc setAttributedText:[str setDifferentColorWithMarkStr:strMark lineSpacing:4 alignment:NSTextAlignmentCenter color:[UIColor colorWithHexString:@"#F5B031"]]];
    [self.labelDesc sizeToFit];
    self.labelDesc.centerXTop = XY(self.viewBack.width/2, self.labelBankCodeTitle.bottom+26);
    
    self.btnAhora.centerXTop = XY(self.viewBack.width/2, self.labelDesc.bottom+26);
    
    self.btnAgreement.selected = true;
    [self resetAgreement];
    
    self.viewBack.height = self.btnAgreement.bottom+22;
    self.viewBack.centerXCenterY = XY(SCREEN_WIDTH/2, SCREEN_HEIGHT/2);
}

- (void)resetAgreement{
    NSString * str = self.btnAgreement.selected == false ? @"Sin esperas, retiro más rápido después de liquidar" : @"Préstamo automático activado";
    [self.labelAgreement fitTitle:str variable:0];
    self.btnAgreement.leftTop = XY((self.viewBack.width-self.btnAgreement.width-self.labelAgreement.width)/2, self.btnAhora.bottom+20);
    self.labelAgreement.leftCenterY = XY(self.btnAgreement.right, self.btnAgreement.centerY);
}


@end
